package com.ttl.member;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.ttl.bc.master.department.BCDepartmentDAO;
import com.ttl.bc.master.itemunit.BCItemUnitDAO;
import com.ttl.bc.master.project.BCProjectDAO;
import com.ttl.bc.master.shelf.BCShelfDAO;
import com.ttl.bc.master.warehouse.BCWarehouseDAO;
import com.ttl.common.usermenu.Menu2Bean;
import com.ttl.common.usermenu.UserBean;
import com.ttl.common.usermenu.UserMenuDAO;
import com.ttl.common.util.CommonUtils;

public class LoginAction extends DispatchAction {
    // Global Forwards
    public static final String GLOBAL_FORWARD_getName = "getName";
    
    // Local Forwards
    @SuppressWarnings("unused")
	private static final String FORWARD_sayhello = "sayhello";

    public ActionForward blank(ActionMapping mapping, ActionForm form, 
    		HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		System.out.println(CommonUtils.getDateTimeForStamp()+" #use :LoginAction #mode :blank");    	
	    return mapping.findForward("blank_login");
    }    
    
     
	@SuppressWarnings("rawtypes")
	public ActionForward check(ActionMapping mapping, ActionForm form, 
    		HttpServletRequest request, HttpServletResponse response) throws Exception {
    	
    	LoginForm loginForm = (LoginForm)form;
        String usr = loginForm.getUserName();//((LoginForm)form).getUserName();
        String pwd = loginForm.getPassword();//((LoginForm)form).getPassword();
                
//	    MenuDAO daoN = new MenuDAO();//add         
//	    String hql = "FROM MenuBean m order by m.id";//add
//	    List menuNew = daoN.find(hql);//add                    
//        MenuRepository repository = MenuCreate.create(request, response, menuNew);        
//        MemberDAO dao = new MemberDAO();
//        List<String> result = dao.find(usr,pwd);
        
        UserMenuDAO dao = new UserMenuDAO();
        String hql = "from UserBean where userName='"+usr+"' and password='"+pwd+"'";

        List result = dao.find(hql);        

        if (result!=null && result.size()>0) {
        	UserBean userBean = (UserBean)result.get(0);
        	
        	Iterator it = userBean.getMenus().iterator();

            while(it.hasNext()) {
            	@SuppressWarnings("unused")
				Menu2Bean mb = (Menu2Bean)it.next();
            }        	
        	
	        request.getSession().setAttribute("userName", userBean.getUserName());	        
	        request.getSession().setAttribute("userMenu", userBean.getMenus());
	        request.getSession().setAttribute("departCode", userBean.getDepartCode());
	        request.getSession().setAttribute("preDocNo", userBean.getPreDocNo());
	        request.getSession().setAttribute("userLevel", userBean.getUserLevel());	        	        
	        
//	        request.getSession().setAttribute("repository", repository);
	        
	        //สร้าง BCItem - CommonUtils.writeBCItemFileCode();
	        //CommonUtils.writeBCItemFileCodeAndName();
	        //สร้าง BCProject -CommonUtils.writeBCProjectFileCode();
	        //สร้าง BCDepartment - CommonUtils.writeBCDepartmentFileCode();	        
	      //สร้าง BCDepartment ที่มี name - CommonUtils.writeBCDepartmentFileCodeName();
	      //สร้าง BCAP ที่มี name - CommonUtils.writeBCAPFileCodeName();
	      //สร้าง BCAP ที่มีแต่ code - CommonUtils.writeBCAPFileCode();
	        sessionVariable(request);
	        
			System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+usr+" #use :LoginAction #mode :check  -> success!!");
			
			//
//			List al =  new TestTableDAO().find("From TestTable");
//			for (Object object : al) {
//				TestTable t = (TestTable)object;
//				System.out.print(t.getId()+"\t\t");
//				System.out.println(t.getName());
//			}
			//
			
			
	        return mapping.findForward("success");
	        
        }else {
        	System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+usr+" #use :LoginAction #mode :check  -> fail!!");
        	loginForm.addWarning("Missing");
        	return mapping.findForward("fail");
        }        
    }

	private void sessionVariable(HttpServletRequest request) {
		// TODO Auto-generated method stub
		//request.getSession().setAttribute("apList", new BCAPDAO().findAll());
		String user = (String)request.getSession().getAttribute("userName");
		
		request.getSession().setAttribute("userInDepartmentList", new BCDepartmentDAO().findAll_userInDept(user));
		
		request.getSession().setAttribute("departmentList", new BCDepartmentDAO().findAll());
		request.getSession().setAttribute("projectList", new BCProjectDAO().findAll());
		
		//request.getSession().setAttribute("itemList", new BCItemDAO().findAll());
		request.getSession().setAttribute("warehouseList", new BCWarehouseDAO().findAll());
		request.getSession().setAttribute("shelfList", new BCShelfDAO().findAll());
		request.getSession().setAttribute("unitCodeList", new BCItemUnitDAO().findAll());
		
		request.getSession().setAttribute("docDateNow", CommonUtils.getCurrentDateFormat("yyyy-mm-dd"));
		
	}
}
